<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>相册上传测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .upload-area {
            border: 2px dashed #ccc;
            padding: 20px;
            text-align: center;
            margin: 20px 0;
        }
        .preview {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin: 20px 0;
        }
        .preview img {
            width: 100px;
            height: 100px;
            object-fit: cover;
            border: 1px solid #ccc;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            cursor: pointer;
            margin: 5px;
        }
        button:disabled {
            background: #ccc;
        }
        .status {
            margin: 20px 0;
            padding: 10px;
            border-radius: 5px;
        }
        .success { background: #d4edda; color: #155724; }
        .error { background: #f8d7da; color: #721c24; }
    </style>
</head>
<body>
    <h1>相册上传测试</h1>
    
    <div class="upload-area">
        <input type="file" id="fileInput" multiple accept="image/*">
        <p>选择多张图片进行上传测试</p>
    </div>
    
    <div class="preview" id="preview"></div>
    
    <button id="uploadBtn" disabled>上传相册</button>
    <button id="refreshBtn">刷新相册</button>
    
    <div id="status"></div>
    
    <div id="gallery">
        <h2>已上传的图片</h2>
        <div id="galleryList"></div>
    </div>

    <script>
        const fileInput = document.getElementById('fileInput');
        const uploadBtn = document.getElementById('uploadBtn');
        const refreshBtn = document.getElementById('refreshBtn');
        const preview = document.getElementById('preview');
        const status = document.getElementById('status');
        const galleryList = document.getElementById('galleryList');
        
        let selectedFiles = [];
        
        fileInput.addEventListener('change', (e) => {
            selectedFiles = Array.from(e.target.files).filter(f => f.type.startsWith('image/'));
            preview.innerHTML = '';
            
            selectedFiles.forEach(file => {
                const reader = new FileReader();
                reader.onload = (e) => {
                    const img = document.createElement('img');
                    img.src = e.target.result;
                    preview.appendChild(img);
                };
                reader.readAsDataURL(file);
            });
            
            uploadBtn.disabled = selectedFiles.length === 0;
        });
        
        uploadBtn.addEventListener('click', async () => {
            if (selectedFiles.length === 0) return;
            
            const formData = new FormData();
            selectedFiles.forEach(file => {
                formData.append('files', file);
            });
            
            try {
                uploadBtn.disabled = true;
                uploadBtn.textContent = '上传中...';
                
                const response = await fetch('/api/pic/saveAlbum', {
                    method: 'POST',
                    body: formData
                });
                
                if (response.ok) {
                    const results = await response.json();
                    showStatus('上传成功！', 'success');
                    console.log('上传结果:', results);
                    loadGallery();
                    selectedFiles = [];
                    preview.innerHTML = '';
                    fileInput.value = '';
                    uploadBtn.disabled = true;
                } else {
                    throw new Error(`上传失败: ${response.status}`);
                }
            } catch (error) {
                showStatus(`上传错误: ${error.message}`, 'error');
            } finally {
                uploadBtn.disabled = false;
                uploadBtn.textContent = '上传相册';
            }
        });
        
        refreshBtn.addEventListener('click', loadGallery);
        
        async function loadGallery() {
            try {
                const response = await fetch('/api/pic/list');
                if (response.ok) {
                    const files = await response.json();
                    displayGallery(files);
                } else {
                    throw new Error('获取相册列表失败');
                }
            } catch (error) {
                showStatus(`获取相册失败: ${error.message}`, 'error');
            }
        }
        
        function displayGallery(files) {
            galleryList.innerHTML = '';
            
            if (files.length === 0) {
                galleryList.innerHTML = '<p>暂无图片</p>';
                return;
            }
            
            files.forEach(filename => {
                const div = document.createElement('div');
                div.style.cssText = 'display: inline-block; margin: 10px; text-align: center;';
                div.innerHTML = `
                    <img src="/uploads/${filename}" alt="${filename}" style="width: 150px; height: 150px; object-fit: cover; border: 1px solid #ccc;">
                    <p>${filename}</p>
                `;
                galleryList.appendChild(div);
            });
        }
        
        function showStatus(message, type) {
            status.innerHTML = `<div class="${type}">${message}</div>`;
            setTimeout(() => {
                status.innerHTML = '';
            }, 3000);
        }
        
        // 页面加载时获取相册
        loadGallery();
    </script>
</body>
</html> 